package Servlet;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.org.apache.xml.internal.resolver.Catalog;

import Action.Category;
import Login.CheckLogin;
import Utils.Common;
import Utils.Constant;
import Utils.DbAccess;

import Utils.ZipResponse;

public class DoAdmin extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 4194452284080389577L;
	
	DbAccess db = null;
	
	public void init() {
		try {
			db = new DbAccess(Constant.DB,true);
		} catch ( Exception e) {
			e.printStackTrace();
		}
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		process(req,res);
	}
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		process(req,res);
	}

	private void process(HttpServletRequest req, HttpServletResponse res) throws IOException {
		res.setContentType("text/xml");
		req.setCharacterEncoding("UTF-8");
		res.setCharacterEncoding("UTF-8");
		String action = req.getParameter("act");
		if(!CheckLogin.check(req)){
			ZipResponse.write(req, res, "notlogin", "UTF-8");
			return;
		}
		String userid = req.getSession().getAttribute("userid").toString();
	
		String xml="";
		Connection conn = null;
		try{
			conn = db.getConn();
	
			if (action.equals("getCategoryList")){
				xml = new Category().getCategoryList(conn);
			}
			
			ZipResponse.write(req, res, xml, "UTF-8");
		}catch (Exception e) {
			
		}finally{
			if(conn!=null){try{conn.close();}catch (Exception e) {
			}}
		}

	}
	
}
